import React, { Component, useEffect, useState } from "react";
import { View } from "@tarojs/components";
import classNames from "classnames";

import "./index.scss";
import { ShareHeader, ShareInfoType } from "../../components/share-header";
import Taro, { useRouter } from "@tarojs/taro";
import ProjectCheckDetail from "../project-check-detail";
import {
  fetchTaskRecord,
  fetchCheckShareInfo,
  CheckCheckShareInfo,
} from "../../api";
import { getOssUrl } from "../../utils";
import { isSafari } from "../../../src/utils/check-browser";

const CheckIndex = () => {
  const router = useRouter();
  const shareId = router?.params?.shareId || "";
  const spaceId = router?.params?.spaceId || "";
  const listingId = router?.params?.listingId || "";
  const [shareInfo, setShareInfo] = useState<ShareInfoType>();
  useEffect(() => {
    const init = async () => {
      const [getCheckShareInfoRes, getCheckInfoRes] = await Promise.all([
        fetchCheckShareInfo(spaceId, listingId, shareId),
        CheckCheckShareInfo(spaceId, listingId, shareId, {}),
      ]);
      let companyInfo, creatorInfo;
      if (getCheckShareInfoRes?.code === 0) {
        const data = getCheckShareInfoRes?.data;
        companyInfo = {
          img: getOssUrl({
            bucketName: data?.company?.logo?.bucketName,
            path: data?.company?.logo?.path,
          }),
          name: data?.company?.name,
        };
        console.log(companyInfo);
      }

      if (getCheckInfoRes?.code === 0) {
        const data = getCheckInfoRes?.data?.creator;
        creatorInfo = {
          img: getOssUrl({
            bucketName: data?.img?.bucketName,
            path: data?.img?.path,
          }),
          name: data?.nickName,
        };
        Taro.setStorageSync("accessKey", getCheckInfoRes?.data?.accessKey);
      } else {
        alert(getCheckInfoRes?.message);
      }
      setShareInfo({
        companyInfo,
        creatorInfo,
      });
    };
    init();
  }, []);

  return (
    <View className="check-index">
      <ShareHeader shareInfo={shareInfo as ShareInfoType} title="确认清单" />
      <ProjectCheckDetail withHeader={false} />
      {isSafari() && (
        <div
          className={classNames("bottom-div", {
            "bottom-50": true,
          })}
        />
      )}
    </View>
  );
};

export default CheckIndex;
